package com.hyt.it.ogt.kq.main.controller.bm;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.cj.model.vo.OptionTree;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.common.config.exception.InvalidRequestException;
import com.hyt.it.ogt.kq.common.utils.KqStringUtils;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.model.dto.CopyUserPermissionDTO;
import com.hyt.it.ogt.kq.service.bm.model.vo.UserPermissionInfo;
import com.hyt.it.ogt.kq.service.bm.model.vo.UserPermissionVo;
import com.hyt.it.ogt.kq.service.bm.service.IUserPermissionService;
import com.hyt.log.annotation.Log;
import com.hyt.model.PageParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liteng
 * @since 2021-03-01
 */
@Slf4j
@RestController
@Api(tags = "14.数据权限接口", value = "数据权限接口")
@ApiSort(value = 14)
@RequestMapping("/bm/user-permission")
public class UserPermissionController extends BaseController {

    @Resource
    IUserPermissionService userPermissionService;

    @ApiOperation(value = "14.1.1 保存数据权限信息(保存，下一步)", notes = "(保存，下一步)20000:成功；31141101：用户或者数据权限为空；31141102：当前报名无区域可授权；")
    @ApiOperationSort(value = 1)
    @Log(module = "数据权限管理", business = "添加数据权限信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<?> saveUserPermission(@Validated @RequestBody UserPermissionVo vo, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                return ApiResponse.builder()
                        .code(31141101)
                        .data(bindingResult.getAllErrors())
                        .build();
            }

            userPermissionService.saveUserPermission(vo);
        } catch (InvalidRequestException e) {
            return ApiResponse.builder()
                    .code(e.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 保存数据权限信息异常", e);
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010101).build();
        }
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(vo)
                .build();
    }

    @ApiOperation(value = "14.1.2 查询数据权限分页数据", notes = "20000:成功,31141201：查询数据权限分页数据异常；")
    @ApiOperationSort(value = 2)
    @ThrowsAdvice(code = 31141201, print = "查询数据权限分页数据异常")
    @RequestMapping(value = "/queryPage", method = RequestMethod.GET)
    public ApiResponse<IPage<UserPermissionInfo>> queryPage(PageParam<UserPermissionInfo> pageParam,
                                                            @ApiParam(value = "报名ID", required = true) @RequestParam String projectId,
                                                            @ApiParam("用户ID集合") @RequestParam(required = false) List<String> userIds,
                                                            @ApiParam("区域code集合") @RequestParam(required = false) List<String> areaCodes) {
        return ApiResponse.<IPage<UserPermissionInfo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(userPermissionService.queryPermissionPage(pageParam, projectId, userIds, areaCodes))
                .build();
    }

    @ApiOperation(value = "14.1.3 查询数据权限信息", notes = "20000:成功；31141302：当前报名无区域可授权；31141301：查询数据权限信息异常；")
    @ApiOperationSort(value = 3)
    @RequestMapping(value = "/loadPermission", method = RequestMethod.GET)
    public ApiResponse<List<OptionTree>> loadPermission(String projectId) {
        try {
            List<OptionTree> list = userPermissionService.loadPermissionByUserId(projectId);
            return ApiResponse.<List<OptionTree>>builder()
                    .code(Constant.RESPONSE_CODE_SUCCESS)
                    .data(list)
                    .build();
        } catch (InvalidRequestException e) {
            return ApiResponse.<List<OptionTree>>builder()
                    .code(e.getCode())
                    .build();
        } catch (Exception e) {
            log.error("# 14.1.3 查询数据权限信息异常", e);
            return ApiResponse.<List<OptionTree>>builder()
                    .code(31141301)
                    .build();
        }
    }

    @ApiOperation(value = "14.1.4 删除数据权限信息(保存，下一步)", notes = "(保存，下一步)20000:成功；31141401：删除数据权限信息异常；")
    @ApiOperationSort(value = 4)
    @Log(module = "数据权限管理", business = "删除数据权限信息")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Object deleteUserPermission(String projectId, String userId) {
        try {
            if (!KqStringUtils.isEmpty(projectId) && !KqStringUtils.isEmpty(userId)) {
                userPermissionService.deleteUserPermission(projectId, userId);
            }
        } catch (Exception e) {
            log.error("# 14.1.4 删除数据权限信息(保存，下一步)异常", e);
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31141401).build();
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
    }

    @ApiOperation(value = "14.1.5 复制数据权限信息", notes = "20000:成功；31141501：复制数据权限信息异常；")
    @ApiOperationSort(value = 5)
    @Log(module = "数据权限管理", business = "复制数据权限信息")
    @RequestMapping(value = "/copyUserPermission", method = RequestMethod.POST)
    public Object copyUserPermission(@RequestBody CopyUserPermissionDTO copyUserPermissionDTO) {
        try {
            userPermissionService.copyUserPermission(copyUserPermissionDTO.getOldProjectId(), copyUserPermissionDTO.getNewProjectId());
        } catch (Exception e) {
            log.error("# 14.1.5 复制数据权限信息异常", e);
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31141501).build();
        }
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
    }

}
